package chapter1.listSet.task;

import java.util.*;

/*
4:baidu ArrayList和LinkedList实现原理，并比较性能（操作加入新值100000次，随机删除100个值，比较时间）
 */
public class Task4 {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new LinkedList<>();
        Random random = new Random();
        for (int i = 0; i < 100000; i++) {
            list1.add("ABC" + i);
            list2.add("ABC" + i);
        }
        System.out.println("ArrayList对象的长度：" + list1.size() + "，LinkedList对象的长度：" + list2.size());
        long begin1 = System.currentTimeMillis();
        while (list1.size() > 99900) {
            try {
                list1.remove(random.nextInt(100001));
            } catch (Exception e) {
            }
        }
        long end1 = System.currentTimeMillis();
        System.out.println("ArrayList对象的长度：" + list1.size() + "，随机删除100个值耗时" + (end1 - begin1) + "毫秒");


        long begin2 = System.currentTimeMillis();
        while (list2.size() > 99900) {
            try {
                list2.remove(random.nextInt(100001));
            } catch (Exception e) {
            }
        }
        long end2 = System.currentTimeMillis();
        System.out.println("LinkedList对象的长度：" + list2.size() + "，随机删除100个值耗时" + (end2 - begin2) + "毫秒");
    }
}
